﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Collections;
using FooFun.DataElements.Dtos;

namespace PhysicalDataProvider
{
    public class AccountService
    {
        string[] selectCols = new string[] { "AccountId", "AccountName", "Description", "Theme" };

        string table = "t_Accounts";

        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        public IDataElement ListDataElement()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, table, null);

            return dbDE;
        }

        public IDataElement ListAccount()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, table, "Status = 0");
            AccountListDataElement list = new AccountListDataElement(dbDE);

            return list;
        }

        class AccountDataElement : CustomDataElement, IEnumItem
        {
            public AccountDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "AccountId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }

            #region IEnumItem 成员

            public string Name
            {
                get
                {
                    return Convert.ToString(this["AccountName"]);
                }
            }

            public Guid Id
            {
                get
                {
                    return DataProviderUtil.GetGuid
                        (this["AccountId"].Value);
                }
            }

            #endregion
        }

        class AccountListDataElement : CustomDataElement
        {
            public AccountListDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IEnumerator GetEnumerator()
            {
                foreach (IDataElement item in this.InnerDataElement)
                {
                    yield return new AccountDataElement(item);
                }
            }
        }
    }
}
